package com.example.controller;

import com.example.pojo.GoodsCate;
import com.example.pojo.Result;
import com.example.service.GoodsCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods-cate")
public class GoodsCateController {

    @Autowired
    private GoodsCateService goodsCateService;

    // 新增商品分类
    @PostMapping
    public Result add(@RequestBody @Validated(GoodsCate.Add.class) GoodsCate goodsCate) {
        goodsCateService.add(goodsCate);
        return Result.success();
    }

    // 查询所有商品分类列表
    @GetMapping
    public Result<List<GoodsCate>> list() {
        List<GoodsCate> list = goodsCateService.list();
        return Result.success(list);
    }

    // 查看分类详情
    @GetMapping("/detail")
    public Result<GoodsCate> detail(@RequestParam Integer id) {
        GoodsCate goodsCate = goodsCateService.findById(id);
        return Result.success(goodsCate);
    }

    @PutMapping
    public Result update(@RequestParam Integer id, @RequestBody @Validated(GoodsCate.Update.class) GoodsCate goodsCate) {
        goodsCate.setId(id); // 确保设置ID
        goodsCateService.update(goodsCate);
        return Result.success();
    }

    // 删除商品分类
    @DeleteMapping
    public Result delete(@RequestParam @Validated(GoodsCate.Delete.class) Integer id) {
        goodsCateService.delete(id);
        return Result.success();
    }
}